package com.fast.admin.controller;


import cn.hutool.core.util.RandomUtil;
import com.fast.admin.service.SysUserService;
import com.fast.admin.utils.SendCodeUtils;
import com.fast.common.em.LoginTypeEm;
import com.fast.common.pojo.req.admin.SysUserReq;
import com.fast.common.pojo.resp.admin.SysUserResp;
import com.fast.common.web.annotation.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 认证
 *
 * @author : TWL
 * @createTime : 2024/7/14 1:03
 */
@Slf4j
@RestController
@ResponseResult
@RequestMapping("auth")
public class authController {


    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SendCodeUtils sendCodeUtils;

    /**
     * 发送验证码 先用邮箱
     */
    @PostMapping("/sendCode")
    public boolean sendCode(@RequestBody SysUserReq authUser) {
        Assert.notNull(authUser.getEmail(), "请输入邮箱");
        String code = RandomUtil.randomNumbers(5);
        log.info("开始发送邮件");
        sendCodeUtils.sendCodeOfMail(authUser.getEmail(), code);
        return true;
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public SysUserResp login(@RequestBody SysUserReq authUser) {
        LoginTypeEm code = LoginTypeEm.getCode(authUser.getType());
        Assert.notNull(code, "非法操作");
        switch (code) {
            case MAIL -> {
                return sysUserService.mailLogin(authUser);
            }
            case IPHONE -> {
                return sysUserService.iphoneLogin(authUser);
            }
            case WEICHAT -> {
                return sysUserService.weiChatLogin(authUser);
            }
            default -> {
                return sysUserService.passwordLogin(authUser);
            }
        }
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public void register(@RequestBody  @Validated SysUserReq authUser) {
        sysUserService.register(authUser);
    }



}
